<template>
  <el-form
      label-position="left"
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="150px"
      class="demo-ruleForm"
  >
    <el-card class="box-card" shadow="never">
      <h4>计算标题</h4>
      <el-form-item label="计算标题" prop="title.value" :rules="rules.title"   >
        <el-input autocomplete="off" v-model="ruleForm.title.value" />
      </el-form-item>
    </el-card>
    <el-card class="box-card" shadow="never">
      <h4>上游计算数据库</h4>
      <el-form-item label="数据库文件名称" prop="Database.value" >
        <el-select :change="ruleForm.Database.onChange()"  placeholder="Select" v-model="ruleForm.Database.value">
          <el-option label="无数据库" value="0" />
          <el-option label="数据库1" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="水池天数" prop="PoolWaterDays.value"  :rules="rules.poolWaterDays" style="max-width: 370px">
        <el-input v-model.number="ruleForm.PoolWaterDays.value" :disabled="ruleForm.PoolWaterDays.ifBan"/>
      </el-form-item>
    </el-card>

    <el-card class="box-card" shadow="never">
      <h4>堆芯计算选项</h4>
      <el-form-item label="功率重构" prop="PowerReconstruction.value" >
        <el-select  class="m-2" placeholder="Select" v-model="ruleForm.PowerReconstruction.value">
          <el-option label="是" value="true"></el-option>
          <el-option label="否" value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="反应模式" prop="ReactiveMode.value" >
        <el-select  class="m-2" placeholder="Select" v-model="ruleForm.ReactiveMode.value" >
<!--          全是;全否;仅慢催化剂反馈;仅多普勒反馈-->
          <el-option label="全是" value="全是"></el-option>
          <el-option label="全否" value="全否"></el-option>
          <el-option label="仅慢催化剂反馈" value="仅慢催化剂反馈"></el-option>
          <el-option label="仅多普勒反馈" value="仅多普勒反馈"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="临界搜索" prop="CriticalSearch.value">
        <el-select  class="m-2" placeholder="Select" v-model="ruleForm.CriticalSearch.value" @change="ruleForm.CriticalSearch.onChange()">
          <el-option label="调硼临界搜索" value="调硼临界搜索"></el-option>
          <el-option label="调棒临界搜索" value="调棒临界搜索"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="重新定义提棒顺序" prop="RedefineOrder.value">
        <el-select  class="m-2" placeholder="Select" @change="ruleForm.RedefineOrder.onChange()" :disabled="ruleForm.RedefineOrder.ifBan" v-model="ruleForm.RedefineOrder.value">
          <el-option label="是" value="true"></el-option>
          <el-option label="否" value="false"></el-option>
        </el-select>
        <el-button type="primary" :disabled="ruleForm.editButton.ifBan" style="margin-left: 16px" @click="drawer = true">编辑</el-button>
        <el-drawer v-model="drawer" title="重新定义提棒顺序" :with-header="false" size="60%">
          <el-form-item label="控制提棒步数" prop="drawer.step.value" >
            <el-input autocomplete="off" v-model.number="ruleForm.drawer.step.value" :rules="rules.drawer" />
          </el-form-item>
          <div style="margin:20px"/>
          <el-form-item label="控制提棒顺序" prop="drawer.oder.value" :rules="rules.drawer" >
            <el-input autocomplete="off" v-model="ruleForm.drawer.oder.value" />
          </el-form-item>
          <div style="margin:20px"/>
          <el-form-item label="控制最终棒位" prop="drawer.end.value"  >
            <el-input autocomplete="off" v-model="ruleForm.drawer.end.value" :rules="rules.drawer" />
          </el-form-item>
          <div style="margin:20px"/>
          <el-form-item label="控制初始棒位" prop="drawer.start.value"  >
            <el-input autocomplete="off" v-model="ruleForm.drawer.start.value" :rules="rules.drawer" />
          </el-form-item>
          <div style="margin:20px"/>
          <el-form-item label="是否定义重叠棒" style="max-width: 370px" prop="drawer.redefine.value">
            <el-select  class="m-2" placeholder="Select" @change="ruleForm.drawer.redefine.onChange()"  v-model="ruleForm.drawer.redefine.value">
              <el-option label="是" value="true"></el-option>
              <el-option label="否" value="false"></el-option>
            </el-select>
          </el-form-item>
          <DrawerTable v-model:values="ruleForm.drawer.table.value" :on-add-item="ruleForm.drawer.table.onAddItem" :delete-row="ruleForm.drawer.table.deleteRows" :disabled="ruleForm.drawer.table.ifBan" :onChange="(i,key,value)=>{ruleForm.drawer.table.value[i][key]=value}"></DrawerTable>
        </el-drawer>
      </el-form-item>
      <el-form-item label="keff目标值" prop="KeffValue.value" style="max-width: 370px" :rules="rules.number">
        <el-input :disabled="ruleForm.KeffValue.ifBan" v-model="ruleForm.KeffValue.value" />
      </el-form-item>
      <el-form-item label="是否进行寿期搜索" style="max-width: 370px" prop="IfLifeSearch.value">
        <el-select  class="m-2" placeholder="Select" @change="ruleForm.IfLifeSearch.onChange()" :disabled="ruleForm.IfLifeSearch.ifBan" v-model="ruleForm.IfLifeSearch.value">
          <el-option label="是" value="true"></el-option>
          <el-option label="否" value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="氙选项" prop="xenon.value">
        <el-select class="m-2" placeholder="Select" :disabled="ruleForm.xenon.ifBan" v-model="ruleForm.xenon.value">
          <el-option label="平衡氙" value="平衡氙"></el-option>
          <el-option label="最大氙" value="最大氙"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="寿期末keff" prop="LifeEndKeff.value" :rules="rules.number" style="max-width: 370px">
        <el-input :disabled="ruleForm.LifeEndKeff.ifBan" v-model="ruleForm.LifeEndKeff.value" />
      </el-form-item>
      <el-form-item label="寿期末硼浓度最小值" prop="LifeEndBoronConcentrationMin.value" :rules="rules.number" style="max-width: 370px">
        <el-input :disabled="ruleForm.LifeEndBoronConcentrationMin.ifBan" v-model="ruleForm.LifeEndBoronConcentrationMin.value"/>
      </el-form-item>
      <el-form-item label="搜索精度(绝对值)" prop="SearchAccuracy.value" :rules="rules.number" style="max-width: 370px">
        <el-input :disabled="ruleForm.SearchAccuracy.ifBan" v-model="ruleForm.SearchAccuracy.value" />
      </el-form-item>

    </el-card>
    <el-card class="box-card" shadow="never">
      <h4>咬量计算设置</h4>
      <el-form-item label="是否进行咬量计算" prop="IfBiteCalculation.value">
        <el-select  class="m-2" placeholder="Select" :change="ruleForm.IfBiteCalculation.onChange()"  :disabled="ruleForm.IfBiteCalculation.ifBan" v-model="ruleForm.IfBiteCalculation.value">
          <el-option value="true" label="是"></el-option>
          <el-option value="false" label="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最小的棒微分价值(pcm/步)" prop="MinPCM.value" :rules="rules.number" style="max-width: 370px">
        <el-input :disabled="ruleForm.MinPCM.ifBan"  v-model="ruleForm.MinPCM.value" />
      </el-form-item>
      <el-form-item label="主调节棒组位置选项" prop="MainRegulatingRodLocationSelect.value">
        <el-select  class="m-2" placeholder="Select" :disabled="ruleForm.MainRegulatingRodLocationSelect.ifBan" v-model="ruleForm.MainRegulatingRodLocationSelect.value">
           <el-option value="全提" label="全提"></el-option>
           <el-option value="调节带上限" label="调节带上限"></el-option>
           <el-option value="调节带中间" label="调节带中间"></el-option>
           <el-option value="调节带下限" label="调节带下限"></el-option>
           <el-option value="用户输入" label="用户输入"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="距离咬位置的相对步数" prop="DistanceOfBiteRelativeStep.value" :rules="rules.drawer"  style="max-width: 370px">
        <el-input :disabled="ruleForm.DistanceOfBiteRelativeStep.ifBan" v-model="ruleForm.DistanceOfBiteRelativeStep.value" />
      </el-form-item>
      <el-form-item label="主调节棒组位置(插入步)" prop="MainRegulatingRodLocationStep.value" :rules="rules.drawer" style="max-width: 370px">
        <el-input :disabled="ruleForm.MainRegulatingRodLocationStep.ifBan" v-model="ruleForm.MainRegulatingRodLocationStep.value" />
      </el-form-item>
    </el-card>
    <el-card class="box-card" shadow="never">
    <h4>燃耗步数设置</h4>
    <el-form-item label="时间单位" prop="TimeUnit.value" >
      <el-select :disabled="ruleForm.TimeUnit.ifBan" v-model="ruleForm.TimeUnit.value" placeholder="选择">
<!--        选项值{天（EFPD）;天（DAY）;小时;MWd/tU}-->
        <el-option label="天(EFPD)" value="天(EFPD)" />
        <el-option label="天(DAY)" value="天(DAY)" />
        <el-option label="小时" value="小时" />
        <el-option label="MWd/tU" value="MWd/tU" />
      </el-select>
    </el-form-item>
    <el-form-item label="燃耗步数" prop="BurnUpNum.value" :rules="rules.number">
      <el-input-number :disabled="ruleForm.BurnUpNum.ifBan" @change="ruleForm.BurnUpNum.onChange()" v-model="ruleForm.BurnUpNum.value" :min="0" />
    </el-form-item>
    <el-form-item label="燃耗步长" prop="BurnUpStepLen.value" :rules="rules.checkSpecialParam" style="min-width: 80%" >
      <el-input
          v-model="ruleForm.BurnUpStepLen.value"
          autocomplete="off"
          type="textarea"
          rows="5"
      />
    </el-form-item>
      <el-form-item label="硼浓度(ppm)" prop="BoronConcentration.value" :rules="rules.checkSpecialParam"  style="min-width: 80%" >
        <el-input
            :disabled="ruleForm.BoronConcentration.ifBan"
            v-model="ruleForm.BoronConcentration.value"
            autocomplete="off"
            type="textarea"
            rows="5"
        />
      </el-form-item>
      <el-form-item label="每个燃耗步是否写库" prop="IfWriteEveryBurnUpStep.value"  style="min-width: 80%" >
<!--        全写库;全不写库;自定义-->
        <el-select :disabled="ruleForm.IfWriteEveryBurnUpStep.ifBan" v-model="ruleForm.IfWriteEveryBurnUpStep.value" placeholder="请选择">
          <el-option label="全写库" value="全写库" />
          <el-option label="全不写库" value="全不写库" />
          <el-option label="自定义" value="自定义" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否写库(0或1,1表示写库)" prop="IfWrite.value" :rules="rules.checkSpecialParam"  style="min-width: 80%" >
        <el-input
            :disabled="ruleForm.IfWrite.ifBan"
            v-model="ruleForm.IfWrite.value"
            autocomplete="off"
            type="textarea"
            rows="5"
        />
      </el-form-item>

      <el-form-item label="控制棒组棒位" prop="ControlStick.value"  style="min-width: 80%" >
        <el-select @change="ruleForm.ControlStick.onChange()" :disabled="ruleForm.ControlStick.ifBan" v-model="ruleForm.ControlStick.value" placeholder="请选择">
          <el-option label="不改变" value="不改变" />
          <el-option label="全提棒" value="全提棒" />
          <el-option label="全插棒(正常停堆)" value="全插棒(正常停堆)" />
          <el-option label="全插棒(安全停堆)" value="全插棒(安全停堆)" />
          <el-option label="自定义" value="自定义" />
        </el-select>
      </el-form-item>
    <el-form-item>
       <EditTable :disabled="ruleForm.ControlStickValue.ifBan" :values="ruleForm.ControlStickValue.value"></EditTable>
    </el-form-item>
    </el-card>
    <div class="sub_box">
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
      >提交</el-button
      >
      <el-button  @click="saveInfo()"
      >保存数据</el-button>
    </el-form-item>
    </div>
  </el-form>
</template>

<script setup>
import { reactive, ref,onMounted } from 'vue'
import {isFloat, isSpecialParam,isNumber} from "@/utils/checkRule";
import EditTable from '@/components/EditTable.vue'
import DrawerTable from '@/components/DrawerTable.vue'
import {ElMessage} from "element-plus";
const drawer = ref(false)
const ruleFormRef = ref()
const rules = reactive({
  title:[{ required: true, trigger: 'change', message: '请输入标题'}],
  checkSpecialParam: [{required: true, validator: checkSpecialParam, trigger: 'change' }],
  decimals: [{ required: true ,validator: checkFloat, trigger: 'change', message: '请输入小数'}],
  number: [{ required: true ,validator: checkNumber, trigger: 'change', message: '请输入数字'}],
  require:[{required:true,message: '请输入标题'},{min: 1, max: 50,message: '长度1~50'}],
  poolWaterDays: [
    { required: true, message: '请输入' },
  ],
  drawer:[{ min: 0, max: 50,message:'注意长度0~50'}]
})
function checkNumber(rule, value, callback) {
  if (!value) {
     callback(new Error('请输入'))
  }
  if ((value+"").length>15){
    callback(new Error('长度1~15'))
  }
  if (isFloat(value)||isNumber(value)){
       callback()
  }else{
     callback(new Error('请输入数字'))
  }

}
function checkFloat(rule, value, callback) {
  if (!value) {
     callback(new Error('请输入'))
  }
  if ((value+"").length>15){
    callback(new Error('长度1~15'))
  }
  if (!isFloat(value)){
     callback(new Error('请输入浮点数'))
  }else{
     callback()
  }

}
function checkSpecialParam(rule, value, callback){
  if (value === '') {
    callback(new Error('请输入'))
  }
  else if (!isSpecialParam(value,ruleForm.Step)) {
    callback(new Error("请输入正确的格式"))
  } else {
    callback()
  }
}
let ruleForm = reactive({
  title:{value:getInfo(['title','value'])||"",ifBan:false},
  Database:{
    value:getInfo(['Database','value'])||"0",
    ifBan:false,
    onChange:()=>{

    if (ruleForm.Database.value=='0'){
       ruleForm.PoolWaterDays.ifBan=true
       ruleForm.PoolWaterDays.value=0
    }
    else {
      ruleForm.PoolWaterDays.ifBan=false

    }

  }
  },
  PoolWaterDays:{value:getInfo(['PoolWaterDays','value'])||"0" ,ifBan:false},
  PowerReconstruction:{value:getInfo(['PowerReconstruction','value'])||"true",ifBan:false},

  ReactiveMode:{value:getInfo(['ReactiveMode','value'])||"全是",ifBan:false},
  CriticalSearch:{
    value:getInfo(['CriticalSearch','value'])||"调硼临界搜索",
    ifBan:false,
    onChange:()=>{
      switch (ruleForm.CriticalSearch.value) {
        case "调硼临界搜索":ruleForm.RedefineOrder.ifBan=true;ruleForm.KeffValue.ifBan=false;ruleForm.IfLifeSearch.ifBan=false;break;
        case "调棒临界搜索":ruleForm.RedefineOrder.ifBan=false;ruleForm.KeffValue.ifBan=false;ruleForm.IfLifeSearch.ifBan=false;break;
        case "否":ruleForm.RedefineOrder.ifBan=false;ruleForm.KeffValue.ifBan=true;ruleForm.IfLifeSearch.ifBan=true;break;
      }
        // if (ruleForm.CriticalSearch.value=='调硼临界搜索'){
        //      ruleForm.RedefineOrder.ifBan=true
        // }
    }},
  RedefineOrder:{value:getInfo(['RedefineOrder','value'])||"false" ,ifBan:false,onChange:()=>{
    if (ruleForm.RedefineOrder.value=='false'){
        ruleForm.editButton.ifBan=true
    }else {
      ruleForm.editButton.ifBan=false
    }
    }},
  editButton:{value:getInfo(['editButton','value'])||"",ifBan:false},
  KeffValue:{value:getInfo(['KeffValue','value'])||"0",ifBan:false},
  IfLifeSearch:{
    value:getInfo(['IfLifeSearch','value'])||"true"
    ,ifBan:false,
    onChange:()=>{ //有bug
      switch (ruleForm.IfLifeSearch.value+ruleForm.CriticalSearch.value) {
        case "true调硼临界搜索":ruleForm.xenon.ifBan=true;ruleForm.LifeEndKeff.ifBan=true;ruleForm.LifeEndBoronConcentrationMin.ifBan=false;ruleForm.SearchAccuracy.ifBan=true;break;
        case "true调棒临界搜索":ruleForm.xenon.ifBan=false;ruleForm.LifeEndKeff.ifBan=false;ruleForm.LifeEndBoronConcentrationMin.ifBan=true;ruleForm.SearchAccuracy.ifBan=false;break;
        default :ruleForm.xenon.ifBan=true;ruleForm.LifeEndKeff.ifBan=true;ruleForm.LifeEndBoronConcentrationMin.ifBan=true;ruleForm.SearchAccuracy.ifBan=true;break;
      }
    }},
  xenon:{value:getInfo(['xenon','value'])||"平衡氙" ,ifBan:false},
  LifeEndKeff:{value:getInfo(['LifeEndKeff','value'])||"0" ,ifBan:false},
  LifeEndBoronConcentrationMin:{value:getInfo(['LifeEndBoronConcentrationMin','value'])||"0" ,ifBan:false},
  SearchAccuracy:{value:getInfo(['SearchAccuracy','value'])||"0.00" ,ifBan:false},
  IfBiteCalculation:{
    value:getInfo(['IfBiteCalculation','value'])||"true",
    ifBan:false,
    onChange:()=>{
       const change=(value=[false,false,false,false])=>{
         ruleForm.MinPCM.ifBan=value[0];
         ruleForm.MainRegulatingRodLocationSelect.ifBan=value[1];
         ruleForm.DistanceOfBiteRelativeStep.ifBan=value[2];
         ruleForm.MainRegulatingRodLocationStep.ifBan=value[3];
         if (ruleForm.MainRegulatingRodLocationSelect.value==="用户输入"&&value[1]===false){
           ruleForm.DistanceOfBiteRelativeStep.ifBan=false;
         }
       }
       switch (ruleForm.IfBiteCalculation.value) {
         case "true":change([false,false,true,true]);break;
         case "false":change([true,true,true,false]);break;
       }

    }},
  MinPCM:{value:getInfo(['MinPCM','value'])||0,ifBan:false},
  MainRegulatingRodLocationSelect:{value:getInfo(['MainRegulatingRodLocationSelect','value'])||"全提",ifBan:false},
  DistanceOfBiteRelativeStep:{value:getInfo(['DistanceOfBiteRelativeStep','value'])||"0",ifBan:false},
  MainRegulatingRodLocationStep:{value:getInfo(['MainRegulatingRodLocationStep','value'])||"",ifBan:false},
  TimeUnit:{value:getInfo(['TimeUnit','value'])||"天(EFPD)",ifBan:false},
  BurnUpNum:{
    value:getInfo(['BurnUpNum','value'])||"0",
    ifBan:false,
    onChange:()=>{
      let arr=[]
      for (let i = 0; i < parseInt(ruleForm.BurnUpNum.value); i++) {
           arr.push({
             date: i+1+'',
             data1: '0',
             data2: '0',
             data3: '0',
             data4: '0',
             data5: '0',
             data6: '0',
             data7: '0',
             data8: '0',
             data9: '0',
           })
      }
        ruleForm.ControlStickValue.value=arr
        console.log(arr)
    }},
  BurnUpStepLen:{value:getInfo(['BurnUpStepLen','value'])||"",ifBan:false},
  BoronConcentration:{value:getInfo(['BoronConcentration','value'])||"",ifBan:false},
  IfWriteEveryBurnUpStep:{value:getInfo(['IfWriteEveryBurnUpStep','value'])||"全写库",ifBan:false},
  IfWrite:{value:getInfo(['IfWrite','value'])||"",ifBan:false},
  ControlStick:{
    value:getInfo(['ControlStick','value'])||"不改变",
    ifBan:false,
    onChange:()=>{
      if (ruleForm.ControlStick.value==="自定义"){
        ruleForm.ControlStickValue.ifBan=false
      }
      else {
        ruleForm.ControlStickValue.ifBan=true
      }
    }
    },
  ControlStickValue:{value:getInfo(['ControlStickValue','value'])||[],ifBan:true},
  drawer:{
    step:{value:getInfo(['drawer','step','value'])||"",ifBan:false},
    oder:{value:getInfo(['drawer','oder','value'])||"",ifBan:false},
    start:{value:getInfo(['drawer','start','value'])||"",ifBan:false},
    end:{value:getInfo(['drawer','end','value'])||"",ifBan:false},
    redefine:{
      value:getInfo(['drawer','redefine','value'])||true,
      ifBan:false,
      onChange:()=>{
        if (ruleForm.drawer.redefine.value==="true"){
            ruleForm.drawer.table.ifBan=false
        }
        else{
            ruleForm.drawer.table.ifBan=true
        }
      }},
    table:{
      value:getInfo(['drawer','table','value'])||[{
        name:"",
        height:"",
      }],
      ifBan:false,
      onAddItem:()=>{
        console.log("tianjia")
         ruleForm.drawer.table.value.push({
            name:"",
            height:"",
         })
      },
      deleteRows:(index)=>{
        ruleForm.drawer.table.value.splice(index, 1)
      }
    }
  }
})



const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      ElMessage({
        message: '提交成功',
        type: 'success',
      })
      console.log(ruleForm)
    } else {
      ElMessage({
        message: '提交失败',
        type: 'warning',
      })
      console.log('error submit!')
      console.log(ruleForm)

      return false
    }
  })
}

function getInfo(keys=[]){
  if (window.localStorage.getItem("formData")){
    let obj = JSON.parse(window.localStorage.getItem("formData"))
    let value =obj[keys[0]]
    for (let i = 1; i < keys.length; i++) {
      value = value[keys[i]];
    }
    return value
  }
  else {
    return 
  }

}
function  saveInfo(){
    window.localStorage.setItem("formData",JSON.stringify(ruleForm))
   ElMessage({
    message: '保存成功',
    type: 'success',
  })
}
onMounted(()=>{
  //
  //   if (window.localStorage.getItem("formData")){
  // }
  setInterval(saveInfo,60000)
});
</script>
<style scoped>
  .el-card{
    margin: 20px;
  }
  .sub_box{
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20px;
    /*border: 1px solid;*/
  }
</style>
